home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pcb / copydsc2.zip / COPYDESC.C next >
C/C++ Source or Header  |  1992-11-15  |  12KB  |  324 lines

  1. /*  COPYDESC version 1.02        Copyright (C) 1992 by Jim Robeson
  2.  *                                                     11/11/92
  3.  *  Function:
  4.  *    Read a PCBoard DIR LIST file (arg-1),
  5.  *      and copy it to a file (arg-2),
  6.  *      changing all multi-line descriptions to "long lines".
  7.  *      with the output position of the '|' set in arg-3,
  8.  *      and the starting position of the text set in arg-4.
  9.  *    note: requirment:   1 < arg-3 < arg-4 <= 34
  10.  *
  11.  *  Other uses:
  12.  *    None.
  13.  *
  14.  *  Execution:
  15.  *    Run from command line or .BAT:
  16.  *      COPYDESC drv:\path\in-file-name drv:\path\out-file-name |pos txtpos
  17.  *    If run without arguments, a bit of help appears.
  18.  *
  19.  *  Compiled with:
  20.  *    Borland's Turbo C 2.0.
  21.  *    tcc copydesc
  22.  *
  23.  *  Disclaimer:
  24.  *    This program is contributed to the Public Domain.  It can be
  25.  *    freely used, modified and/or distributed by anyone. The only
  26.  *    thing I ask is that you remember that I am not responsible
  27.  *    for anything that might go wrong through the use of this
  28.  *    program.  I have tested the program enough for my own use.
  29.  *    I also realize that bugs can appear in most every program.
  30.  *    Therefore, YOU USE THIS PROGRAM AT YOUR OWN RISK.
  31.  *
  32.  *  To-do:
  33.  *    > See if the blank in split-line hyphenated words can be eliminated.
  34.  *
  35.  *  Enjoy,
  36.  *  Jim Robeson
  37.  *  The Cricket BBS, Pacific Grove CA,  408-373-3773
  38.  */
  39.  
  40. #include "stdio.h"                        /* Standard I/O definitions */
  41. #include "string.h"
  42.  
  43. #define TRUE 1
  44. #define FALSE 0
  45.  
  46. /* test to see if 'ch' is an acceptable DOS filename character */
  47. #define vld_dos(ch)  ( (( (ch) >= 'A' && (ch) <= 'Z' ) || \
  48.                         ( (ch) >= '0' && (ch) <= '9' ) || \
  49.                         (ch) == '!' || \
  50.                         (ch) == '$' || \
  51.                         (ch) == '#' || \
  52.                         (ch) == '@' || \
  53.                         (ch) == '_' || \
  54.                         (ch) == '-' || \
  55.                         (ch) == '{' || \
  56.                         (ch) == '}' || \
  57.                         (ch) == '~' || \
  58.                         (ch) == '%' || \
  59.                         (ch) == '^' || \
  60.                         (ch) == '(' || \
  61.                         (ch) == ')')   \
  62.                        ? 1             \
  63.                        : 0 )
  64. /* test to see if 'ch' is numeric..... <compare   to isdigit(c)> */
  65. #define is_digit(ch)   ( ( (ch) < '0' || (ch) > '9' ) \
  66.                          ? 0                          \
  67.                          : ch )
  68.  
  69. void write_desc ( );             /* prototype */
  70. void save_desc ( );              /* prototype */
  71. void showhelp ( );               /* prototype */
  72.  
  73.   FILE *infile;                  /* the IN file */
  74.   FILE *outfile;                 /* the OUT file */
  75.   char inbuf[BUFSIZ];            /* line buffer for reading from file */
  76.                                  /* BUFSIZ = 512 in stdio.h */
  77.  
  78.   char outbuf[3300];             /* Description Lines save area */
  79.         /* Lets start by assuming 40 max lines coming in. */
  80.         /* Therefore, we need on the output side:         */
  81.         /* a maximum of 80*40 = 3200.                     */
  82.         /* Make it 3300 for safety's sake!                */
  83.         /* Watch this over run itself now!                */
  84.  
  85.   int in_a_desc;         /* working on a description indicator */
  86.   int ipt,opt,linept;    /* index pointers                     */
  87.   int pos_mark;          /* line position of the '|' character */
  88.   int pos_text;          /* starting position of output text   */
  89.  
  90. /* ========================================= */
  91. /* ===  MAIN                             === */
  92. /* ========================================= */
  93. main(int argc, char *argv[])                /* main reads command args  */
  94. {
  95.   char *progname;
  96.   char *filein;
  97.   char *fileout;
  98.   char *adr_mark;
  99.   char *adr_text;
  100.  
  101. /*  Display a little how-to "help" if arguments are null or improper  */
  102.  
  103.   if (argc != 5)           /*  should be 4 args + prognam */
  104.      { if (argc != 1)
  105.          printf("\nERROR: needs 4 arguments\n");
  106.        showhelp();
  107.        exit (1); }               /* exit with errorlevel=1 */
  108.  
  109.   progname = argv[0];
  110.   filein   = argv[1];
  111.   fileout  = argv[2];
  112.  
  113.   adr_mark = argv[3];   /* get arg-3: the position of the output '|' */
  114.   sscanf(&adr_mark[0],"%2d",&pos_mark);
  115.  
  116.   adr_text = argv[4];   /* get arg-4: the 1st position of output text */
  117.   sscanf(&adr_text[0],"%2d",&pos_text);
  118.  
  119.   if (pos_text >= 35)        /* 1st text position must be 34 or less */
  120.      { printf("\nERROR: text position must be less than 35. (arg-4)\n");
  121.        showhelp();
  122.        exit (1); }               /* exit with errorlevel=1 */
  123.  
  124.   if (pos_mark >= pos_text)  /* the '|' must be on the left of the text */
  125.      { printf("\nERROR: '|' position must be less than text position. (arg-3 or arg-4)\n");
  126.        showhelp();
  127.        exit (1); }               /* exit with errorlevel=1 */
  128.  
  129.   if (pos_mark <= 1)         /* the '|' must be 2 or higher */
  130.      { printf("\nERROR: '|' position is 1 or less. (arg-3)\n");
  131.        showhelp();
  132.        exit (1); }               /* exit with errorlevel=1 */
  133.  
  134.  
  135.   printf("\nCOPYDESC IS RUNNING:\n");
  136.   printf("           input = %s, output = %s,\n",filein,fileout);
  137.   printf("           mark = %2d, text = %2d.\n",pos_mark,pos_text);
  138.  
  139.   infile = fopen(filein,"r");          /* open the input file */
  140.   if (infile == NULL)
  141.     {
  142.     fprintf(stderr,"\n\007%s can't open the INPUT file: %s.\n",progname,filein);
  143.     exit (1);
  144.     }
  145.  
  146.   outfile = fopen(fileout,"w");          /* open the output file */
  147.   if (outfile == NULL)
  148.     {
  149.     fprintf(stderr,"\n\007%s can't open the OUTPUT file: %s.\n",progname,fileout);
  150.     exit (1);
  151.     }
  152.  
  153.   in_a_desc = FALSE;      /* say we're not inside a description yet */
  154.  
  155.  /* ========================  copy the file ========================= */
  156.   while ( fgets(inbuf,BUFSIZ,infile) != NULL )  /* TOP-OF-LOOP */
  157.     {
  158.     /* test for line 1 of a file description:     */
  159.     if (vld_dos(inbuf[0]) &&        /* pos 1 valid DOS chars & */
  160.         is_digit(inbuf[20]) &&      /* pos 21 is numeric       */
  161.         inbuf[25] == '-' &&         /* pos 26 = '-'            */
  162.         inbuf[28] == '-')           /* pos 29 = '-'            */
  163.  
  164.       { /* ==== FILE DESCRIPTION LINE # 1 FOUND ======== */
  165.  
  166.         /* if we're already in a description, write the old one */
  167.         if (in_a_desc) write_desc();
  168.         in_a_desc = TRUE;            /* say we are 'inside' */
  169.                                      /* save the 1st line   */
  170.         ipt = 0;
  171.         opt = 0;
  172.         while (inbuf[ipt] != '\0')
  173.           { outbuf[opt] = inbuf[ipt];
  174.             ipt++;
  175.             opt++;
  176.           }
  177.   /* ++++ add a blank to the end for wrap problem */
  178.         opt--;
  179.         while (outbuf[opt] == ' ' || outbuf[opt] == '\n')
  180.             opt--;
  181.         opt++;
  182.         outbuf[opt] = ' ';
  183.         opt++;
  184.         outbuf[opt] = '\n';
  185.         opt++;
  186.  /* ++++ */
  187.         outbuf[opt] = '\0';
  188.         linept = opt;       /* card pointer (1-80), pointing */
  189.         opt--;              /* at the '\n' right now.        */
  190.  
  191.       }   /* ====== end  FILE #1 LINE HERE =============== */
  192.  
  193.     else
  194.  
  195.       {   /* =========== OTHER LINES HERE ================ */
  196.  
  197.         /* if we're already in a description, continue saving data */
  198.         if (in_a_desc) save_desc();
  199.         else                       /* if not, write junk straight out */
  200.           fprintf(outfile,"%s",inbuf);
  201.  
  202.       }   /* ====== end  OTHER LINES HERE ================ */
  203.  
  204.  
  205.     } /* end of while at TOP-OF-LOOP */
  206.  
  207.   /* === Since we dropped through,  ============== */
  208.   /* === implies end of file found. ============== */
  209.  
  210.   if (in_a_desc) write_desc();  /* if we're in a description, write it */
  211.  
  212.   fclose(infile);
  213.   fclose(outfile);
  214.  
  215.   printf("COPYDESC IS FINISHED.\n");
  216.   exit (0);      /* job done, get out, errorvalue = 0 */
  217.  
  218. }  /* ------------- end of main ------------------------------- */
  219.  
  220. /* ========================================= */
  221. /* ===  SHOWHELP                         === */
  222. /* ========================================= */
  223. void showhelp ()
  224. {
  225.   printf("\nCOPYDESC v1.02:\n");
  226.   printf("    Copy PCBoard DIR files (descriptions) converting them to long-liners.\n");
  227.   printf("    by Jim Robeson, 11-15-92\n\n");
  228.   printf("USAGE:\n");
  229.   printf("    COPYDESC drv:\path\in-file-name drv:\path\out-file-name |pos txtpos \n");
  230.   printf("    where  1 < |pos < txtpos <= 34 \n\n");
  231.   return;
  232. }  /* ------------- end of showhelp --------------------------- */
  233.  
  234. /* ========================================= */
  235. /* ===  WRITE_DESC                       === */
  236. /* ========================================= */
  237. void write_desc ()
  238. {
  239.   fprintf(outfile,"%s",outbuf);
  240.   in_a_desc = FALSE;
  241.   return;
  242. }  /* ------------- end of write_desc ------------------------- */
  243.  
  244. /* ========================================= */
  245. /* ===  SAVE_DESC                        === */
  246. /* ========================================= */
  247. /* this is to save all but the first card into the output buffer */
  248. /* the first card is saved in MAIN */
  249. /*                 ipt goes 0-n thru input line, not flexible */
  250. /*                 opt goes 0-x thru output line, set/resets  */
  251. /*                     according to needs                     */
  252. /*                 linept is RELEATIVE to each OUTPUT LINE,   */
  253. /*                     ranging from 1-78 (PCBoard max);       */
  254. /*                     it gets reset as we fill lines.        */
  255. /*                     When coming from line 1, linept points */
  256. /*                     at the '\n' while opt = '\0'           */
  257. void save_desc ()
  258. {
  259.   int skipping;
  260.   int tmpipt;
  261.   int i;
  262.   skipping = TRUE;
  263.  
  264.   ipt = 0;
  265.   while (inbuf[ipt] != '\0')
  266.     {
  267.     if (inbuf[ipt] == '\n')
  268.        { ipt++;               /* skip over any '\n' */
  269.          continue; }
  270.     if (inbuf[ipt] == '|')
  271.        { ipt++;               /* skip over any '|' */
  272.          continue; }
  273.     if (inbuf[ipt] == ' ' && skipping)
  274.        { ipt++;               /* skip over leading blanks */
  275.          continue; }
  276.     skipping = FALSE;
  277.  
  278.     tmpipt = ipt;             /* see if there is room for a word */
  279.     for (i=0; !(inbuf[tmpipt]==' ' || inbuf[tmpipt]=='\n' || inbuf[tmpipt]=='\0' ); tmpipt++) i++;
  280.     if (i+linept > 79)
  281.        { /* do newline wrap first */
  282.        if (outbuf[opt] != '\n') outbuf[opt] = '\n';
  283.        opt++;
  284.        /* run out the '|' character & leading blanks */
  285.        for (linept=1; linept<pos_mark; linept++)
  286.          { outbuf[opt] = ' ';
  287.            opt++;
  288.          }
  289.        outbuf[opt] = '|';
  290.        opt++;
  291.        linept++;
  292.        for (; linept<pos_text; linept++)
  293.          { outbuf[opt] = ' ';
  294.            opt++;
  295.          }
  296.        }
  297.        /* now stuff the word */
  298.        for (; i >= 1; i--)               /* ++++ was >=0 */
  299.           { outbuf[opt] = inbuf[ipt];
  300.             ipt++;
  301.             opt++;
  302.             linept++;
  303.            }
  304.        /* now pop a blank IFF not '\n' nor '\0' */
  305.        if (inbuf[ipt]=='\n') continue;
  306.        if (inbuf[ipt]=='\0') continue;
  307.        for (; inbuf[ipt]==' '; ipt++);
  308.          outbuf[opt] = ' ';
  309.          opt++;
  310.          linept++;
  311.     }
  312.   outbuf[opt] = ' ';
  313.   opt++;
  314.   linept++;
  315.   outbuf[opt] = '\n';
  316.   opt++;
  317.   outbuf[opt] = '\0';
  318.   opt--;
  319.  
  320.   return;
  321. }  /* ------------- end of save_desc -------------------------- */
  322.  
  323. /*------------- End of COPYDESC.C ------------------------------------*/
  324.